/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jili.ubert.code;

import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.Date;

/**
 *
 * @author ChengJiLi
 */
public class CodeTool {
    private static final ByteBuffer buffer = ByteBuffer.allocate(8);

    public static byte[] StringToBit(String str) {
        return str.getBytes();
    }

    public static byte[] StringToBit1(String str) {
        return str.getBytes();
    }

    public static String BitToString(byte[] bit) {
        return new String(bit);
    }

    public static long DateToLong(Date date) {
        return date.getTime();
    }

    public static Date LongToDate(long lng) {
        return new Date(lng);
    }

    public static Date NowDateTime() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public static byte[] IntToBit(int a) {
        return new byte[]{
            (byte) ((a >> 24) & 0xFF),
            (byte) ((a >> 16) & 0xFF),
            (byte) ((a >> 8) & 0xFF),
            (byte) (a & 0xFF)
        };
    }

    public static int BitToInt(byte[] bit) {
        return bit[3] & 0xFF
                | (bit[2] & 0xFF) << 8
                | (bit[1] & 0xFF) << 16
                | (bit[0] & 0xFF) << 24;
    }
    public static byte[] LongToBit(long a){
        buffer.putLong(0, a);  
        return buffer.array();
    }
       
    public static long BitToLong(byte[] bit){
        buffer.put(bit, 0, bit.length);  
       buffer.flip();//need flip   
       return buffer.getLong();
    }

    public static double BitToDouble(byte[] data) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
